package com.ucweb.pooltest.util;

import java.util.HashMap;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Setting {
	private String FILED_SET_ALLOCATE_COUNT = "ALLOCATE";
	private String TABLE_SETTING = "TABLE";
	private int DEFAULT_ALLOCATE_COUNT = 1000;
	private SharedPreferences mSettingPreferences;
	private HashMap<String, Object> mRes;
	private Editor mEditor;
	private static Setting instance;

	private Setting() {
	}

	private Setting(Context context) {
		mSettingPreferences = context.getSharedPreferences(TABLE_SETTING,
				context.MODE_PRIVATE);
		mEditor = mSettingPreferences.edit();
		mRes = new HashMap<String, Object>(1);
	}

	public static Setting getInstance() {
		return getInstance(null);
	}

	public static Setting getInstance(Context context) {
		if (instance == null) {
			instance = new Setting(context);
		}
		return instance;
	}

	public int getAllocateCount() {
		if (mRes.containsKey(FILED_SET_ALLOCATE_COUNT)) {
			return (Integer) mRes.get(FILED_SET_ALLOCATE_COUNT);
		}
		int fileSize = mSettingPreferences.getInt(FILED_SET_ALLOCATE_COUNT,
				DEFAULT_ALLOCATE_COUNT);
		mRes.put(FILED_SET_ALLOCATE_COUNT, fileSize);
		return fileSize;
	}

	public void setAllocateCount(int fileSize) {
		mEditor.putInt(FILED_SET_ALLOCATE_COUNT, fileSize).apply();
		mRes.put(FILED_SET_ALLOCATE_COUNT, fileSize);
	}

}
